﻿using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using OWL.Rendering.HRP;
using ParadoxNotion.Design;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Playables;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class PlayTimeline : LatentActionNodeBase
    {
        public static Action<string> SkipTimeline;
        public static Action<string,double> JumpTimeline;

        [RequiredField]
        public BBParameter<string> timelineName;
        public BBParameter<float> stopTime;
        public BBParameter<bool> stopBgm;
        public BBParameter<DirectorWrapMode> wrapMode = DirectorWrapMode.None;
        public BBParameter<string> path;
        public BBParameter<string> cameraType;
        public BBParameter<float> planeDistance;
        
        /// <summary>
        /// 要使用的渲染场景，为空则不切换
        /// </summary>
        public BBParameter<string> renderSceneName;

        public BBParameter<int> reportEventId;

        [HideInInspector]
        public FlowOutput onStart;
        [HideInInspector]
        public FlowOutput onPreStart;
        //[HideInInspector]
        //public FlowOutput onStop;

        public FlowOutput onSkip;
        
        private bool stopped = false;

        private bool bgmStatus = true;
        
        public override bool allowRoutineQueueing { get { return false; } }

        public IEnumerator Invoke()
        {
            stopped = false;
            SkipTimeline += OnSkipTimeline;
            if(renderSceneName!=null && !string.IsNullOrEmpty(renderSceneName.value))
                HRenderSceneManager.Inst.EnableBaseScene(renderSceneName.value);

            //强制设置为在引导中
            BlueprintUtils.SetIsInGuide(true);
            
            BlueprintUtils.PlayTimeline(timelineName.value, OnStopped, OnStart, OnPreStart, 
                wrapMode.value, path.value, cameraType.value, planeDistance.value);
            if (stopTime.value > 0.01)
            {
                yield return new WaitForSeconds(stopTime.value);
                try
                {
                    BlueprintUtils.StopTimeline(timelineName.value);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            else
            {
                yield return new UnityEngine.WaitUntil(() =>
                {
                    return stopped;
                });
            }
        }

        private void OnStart(PlayableDirector director)
        {
            onStart?.Call(new Flow());
        }

        private void OnPreStart()
        {
            onPreStart?.Call(new Flow());
        }

        private void OnStopped(PlayableDirector director)
        {
            SkipTimeline -= OnSkipTimeline;
            //onStop?.Call(new Flow());
            
            // 不能加在这里，Timeline播放结束了，倒是还没销毁，灯光会突然变暗
            // if (!string.IsNullOrEmpty(renderSceneName.value))
            //     HRenderSceneManager.Inst.DisableBaseScene(renderSceneName.value);

            stopped = true;
            if (reportEventId!=null && reportEventId.value > 0)
            {
                BlueprintUtils.ClientReport(reportEventId.value);
            }
            
            //移除在引导中状态
            BlueprintUtils.SetIsInGuide(false);
        }

        private void OnSkipTimeline(string n)
        {
            if (n == timelineName.value)
            {
                onSkip?.Call(new Flow());
            }
        }

        protected override void OnRegisterDerivedPorts(FlowNode node)
        {
            onPreStart = node.AddFlowOutput("onPrePlay");
            onStart = node.AddFlowOutput("onPlay");
            //onStop = node.AddFlowOutput("onStop");
            onSkip = node.AddFlowOutput("onSkip");
            
            node.AddFlowInput("In", (f) => { Begin(Invoke(), f); });
        }
        
    }
}
